/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.service;

import com.googlecode.jsimplemarket.model.DetaliuPiata;
import com.googlecode.jsimplemarket.model.NStareSpatiuComercial;
import com.googlecode.jsimplemarket.model.SpatiuComercial;
import com.googlecode.jsimplemarket.repository.SpatiuComercialRepository;
import com.googlecode.jsimplemarket.repository.StareSpatiuComercialRepository;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 *
 * @author Alexandru.Codescu
 */
@Service(value = "spatiuComercialService")
public class SpatiuComercialService {
    @PersistenceContext
    private EntityManager entityManager;
    
    @Autowired
    private SpatiuComercialRepository spatiuComercialRepository;
    
    @Autowired
    private StareSpatiuComercialRepository stareSpatiuComercialRepository;
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);                 
        return cal.getTime();
    }
    
    
    private Date getTomorrowCalendar() {
    	GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, 1);        
        return calendar.getTime();
    }
    
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
    
    public List<List<DetaliuPiata>> queryStatisticiSpatiuComercial() {
        Date day30 = getLastMonthDate();
        Date today = getTodayCalendar().getTime();
        Date tommorow = getTomorrowCalendar();
        
        Query query = entityManager.createNamedQuery("queryStatisticiSpatiuComercialByData");
        query.setParameter(1, today);
        query.setParameter(2, day30);
        query.setParameter(3, today);
        query.setParameter(4, day30);
        query.setParameter(5,today);
        query.setParameter(6,tommorow);
        List<Object[]> objects = query.getResultList();
        List<List<DetaliuPiata>> randuriDetalii = new ArrayList<List<DetaliuPiata>>(objects.size());
        List<DetaliuPiata> detalii = new ArrayList<DetaliuPiata>();
        Long idRandCurent = null;
        for (Object[] object : objects) {
            BigInteger idRand = (BigInteger) object[0];
            
            DetaliuPiata detaliuPiata = new DetaliuPiata();
            detaliuPiata.setId(((BigInteger) object[1]).longValue());
            detaliuPiata.setCod((String) object[2]);
            detaliuPiata.setStare((String) object[3]);
            detaliuPiata.setCssClass((String) object[4]);
            detaliuPiata.setIdInchiriere((BigInteger) object[5]);
            detaliuPiata.setFirma((String) object[6]);
            detaliuPiata.setAbsente((BigInteger) object[7]);
            detaliuPiata.setDateInceput((Date) object[8]);
            detaliuPiata.setIzid((BigInteger) object[9]);
            detaliuPiata.setIzdi((Date) object[10]);            
            detaliuPiata.setTid((BigInteger) object[11]);
            detaliuPiata.setTval((BigDecimal) object[12]);
            detaliuPiata.setTcss((String) object[13]);
            
            
            if (idRandCurent != null) {
                if (idRandCurent.equals(idRand.longValue())) {
                } else {
                    randuriDetalii.add(detalii);
                    detalii = new ArrayList<DetaliuPiata>();
                }
            }
            
            detalii.add(detaliuPiata);
            idRandCurent = idRand.longValue();
        }
        randuriDetalii.add(detalii);
        return randuriDetalii;
    }
    
    /**
     * Ruleaza in fiecare zi la ora 00:00:01
     */
//    @Scheduled(cron = "1 0 0 * * *")
//    public void resetareStareSpatiuComercial() {
//        Iterable<SpatiuComercial> spatiiComerciale = spatiuComercialRepository.findAll();
//        NStareSpatiuComercial stareSpatiuComercial = stareSpatiuComercialRepository.findByCod("1");
//        for (SpatiuComercial spatiuComercial : spatiiComerciale) {
//            spatiuComercial.setStareSpatiuComercial(stareSpatiuComercial);
//            spatiuComercialRepository.save(spatiuComercial);
//        }
//    }
}
